package com.coolweather.lechat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import java.util.ArrayList;
import java.util.List;

import Adapter.SectionsPageAdapter;
import Fragments.FragmentChat;
import Fragments.FragmentContact;
import Fragments.FragmentMine;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener, ViewPager.OnPageChangeListener {
    private static final String TAG = "MainActivity";
    private List<Fragment> fragments;
    private ViewPager main_viewPager;
    private BottomNavigationBar main_nav;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        initviewpager();
        initBottomNavBar();
    }

    private void initBottomNavBar() {
        main_nav.setTabSelectedListener(this);
        main_nav.clearAll();
        main_nav.setMode(BottomNavigationBar.MODE_FIXED);
        main_nav.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);
        main_nav.setBarBackgroundColor(R.color.white)
                .setActiveColor(R.color.bluecolor)
                .setInActiveColor(R.color.black);
        main_nav.addItem(new BottomNavigationItem(R.drawable.xiaoxi1,"会话")).setInActiveColor(R.color.black);
        main_nav.addItem(new BottomNavigationItem(R.drawable.yonghu1,"联系人")).setInActiveColor(R.color.black);
        main_nav.addItem(new BottomNavigationItem(R.drawable.dongtai1,"设置")).setInActiveColor(R.color.black);

        main_nav.setFirstSelectedPosition(0);
        main_nav.initialise();
    }

    private void initviewpager() {
        main_viewPager.setOffscreenPageLimit(3);
        fragments = new ArrayList<Fragment>();
        fragments.add(new FragmentChat());
        fragments.add(new FragmentContact());
        fragments.add(new FragmentMine());

        main_viewPager.setAdapter(new SectionsPageAdapter(getSupportFragmentManager(),fragments));

        main_viewPager.addOnPageChangeListener(this);
        main_viewPager.setCurrentItem(0);
    }

    private void initView() {
        main_viewPager = findViewById(R.id.main_viewpage);
        main_nav = findViewById(R.id.main_nav);

    }

    @Override
    public void onTabSelected(int position) {
        main_viewPager.setCurrentItem(position);

    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        main_nav.selectTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}